//
//  QRCodePermissions.m
//  QRCodeDemo
//
//  Created by XZY on 2018/7/23.
//  Copyright © 2018年 xiezongyuan. All rights reserved.
//

#import "QRCodePermissions.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>

@implementation QRCodePermissions
/// 允许使用摄像头
+ (BOOL)allowCamera{
    BOOL isCameraValid = YES;
    //ios7之前系统默认拥有权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
        isCameraValid = NO;
    }
    return isCameraValid;
}

+ (void)requestCamera:(void (^)(BOOL))completion{
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        completion(granted);
    }];
}





/// 允许使用相册
+ (BOOL)allowAlbum{
    PHAuthorizationStatus authorStatus = [PHPhotoLibrary authorizationStatus];
    if ( authorStatus == PHAuthorizationStatusDenied || authorStatus == PHAuthorizationStatusRestricted ) {
        return NO;
    }
    return YES;
}


+ (void)requestAlbum:(void (^)(BOOL))completion{
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        BOOL allowAlbum = YES;
        if ( status == PHAuthorizationStatusDenied || status == PHAuthorizationStatusRestricted ) {
            allowAlbum = NO;
        }
        completion(allowAlbum);
    }];
}
@end
